package com.shxt.framework.rbac.user.web;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.shxt.framework.commons.controller.BaseController;
import com.shxt.framework.commons.exception.ServiceExceiption;
import com.shxt.framework.commons.model.Pager;
import com.shxt.framework.commons.util.MD5Helper;
import com.shxt.framework.rbac.role.service.RoleService;
import com.shxt.framework.rbac.user.model.User;
import com.shxt.framework.rbac.user.service.UserService;

@Controller
@RequestMapping("/sys/user")
public class UserController extends BaseController {
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	//放在形参，都会自动进行实例化
	@GetMapping("/find")
	public String find(Pager pager, Model model) {
		model.addAttribute("pager", this.userService.find(pager));
		model.addAttribute("roleList", this.roleService.getAllRoleList());
		return "user/find";
	}
	@GetMapping("/add")
	public String add(Model model) {
		model.addAttribute("roleList", this.roleService.getAllRoleList());
		return "user/add";
	}
	@PostMapping("/add")
	public String add(User user, Model model) {
		this.userService.add(user);
		return "redirect:/sys/user/find";
	}

	@GetMapping("/valid/account")
	@ResponseBody
	public Map<String, Boolean> validAccount(String account) {
		Map<String, Boolean> map = new HashMap<>();
		try {
			this.userService.validUserAccount(account);
			map.put("flag", true);
		} catch (ServiceExceiption ex) {
			map.put("flag", false);
		}
		return map;
	}
	@GetMapping("/update/{user_id}")
	public String update(@PathVariable Integer user_id, Model model) {
		model.addAttribute("roleList", this.roleService.getAllRoleList());
		model.addAttribute("user", this.userService.load(user_id));
		return "user/update";
	}
	@PutMapping("/update")
	public String update(User user) {
		this.userService.update(user);
		return "redirect:/sys/user/find";
	}

	@GetMapping("/status/update")
	@ResponseBody
	public Map<String, String> updateStatus(User user) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			//判断状态
			/*if (user.getStatus()==2) {
				long time = System.currentTimeMillis();
				time = time + 1000*60*60*2;
				user.setBlocked_date(new Date(time));
			}*/
			this.userService.updateStatus(user);
			map.put("flag", "success");
		} catch (ServiceExceiption ex) {
			map.put("message", ex.getMessage());
		}
		return map;
	}
	@GetMapping("/photo/{user_id}")
	public String photo(@PathVariable int user_id, Model model) {
		model.addAttribute("user", this.userService.load(user_id));
		return "user/photo";
	}
	@PostMapping("/photo")
	public String photo(int user_id, MultipartFile myfile, Model model) {
		try {
			//1.获取服务器的绝对路径
			String path = this.application.getRealPath("/attr/user/");
			//2.建立联系
			File folder = new File(path);
			//3.判断文件或者目录是否存在
			if (!folder.exists()) {
				//3.1创建多级的文件夹
				folder.mkdirs();
			}
			//4.获取上传文件的名称
			String fileName = myfile.getOriginalFilename();
			//5.获取上传文件的后缀名称
			String ext = FilenameUtils.getExtension(fileName);
			//6.创建新的上传文件的名称
			String newFileName = UUID.randomUUID().toString().replaceAll("-", "")+"."+ext;
			//7.完成上传操作
			myfile.transferTo(new File(path+"/"+newFileName));
			//8.完成保存信息到数据库
			User user = new User();
			user.setUser_id(user_id);
			user.setPhoto(newFileName);
			this.userService.update(user);//更新数据库
			//9.提示信息
			model.addAttribute("message", "更新用户头像成功");
		} catch (Exception ex) {
			ex.printStackTrace();//给开发人员使用
			model.addAttribute("message", "更新用户头像失败");
		}
		return "message";
	}

	@GetMapping("/password")
	public String password() {
		return "user/password";
	}

	@GetMapping("/valid/password")
	@ResponseBody
	public Map<String, Boolean> password(String password) {
		Map<String, Boolean> map = new HashMap<>();
		User user = (User) this.session.getAttribute("session_user");
		try {
			this.userService.validUserPassword(password, user.getUser_id());
			map.put("flag", true);
		} catch (ServiceExceiption ex) {
			map.put("flag", false);
		}
		return map;
	}

	@PatchMapping("/password")
	public String updatePassword(String password) {
		User user = (User) this.session.getAttribute("session_user");
		user.setPassword(MD5Helper.MD5(password, 1024));
		this.userService.update(user);

		this.session.invalidate();

		return "redirect:/sys/login";
	}
}
